# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=python3
pkgname=python
pkgver=3.7.4
pkgrel=1
_pybasever=3.7
pkgdesc="Next generation of the python high-level scripting language"
arch=('i686' 'x86_64')
license=('custom')
url="https://www.python.org/"
depends=('libbz2' 'libexpat' 'libffi' 'liblzma' 'ncurses' 'libopenssl' 'libreadline' 'mpdecimal' 'libsqlite' 'zlib')
makedepends=('libbz2-devel' 'libexpat-devel'  'mpdecimal-devel' 'libsqlite-devel' 'libffi-devel' 'ncurses-devel' 'libreadline-devel' 'liblzma-devel' 'openssl-devel' 'zlib-devel')
#optdepends=('tk: for tkinter' 'sqlite')
provides=('python3')
replaces=('python3')
options=('!makeflags')
source=(https://www.python.org/ftp/python/${pkgver%rc*}/Python-${pkgver}.tar.xz
        001-3.4-dbm-cygwin.patch
        002-3.1-enable-new-dtags.patch
        003-3.4-tkinter-cygwin.patch
        004-3.4-ctypes-cygwin.patch
        006-3.1-ncurses-abi6.patch
        007-3.2-export-PySignal_SetWakeupFd.patch
        009-3.2-distutils-shlibext.patch
        010-3.6-pep3149-cygwin.patch
        011-3.6-thread-cygwin64.patch
        012-3.2-getpath-exe-extension.patch
        013-3.4-select-cygwin.patch
        016-3.6-ftm.patch
        017-3.6-mpdec-cygwin.patch
        025-MINGW-compiler-customize-mingw-cygwin-compilers.patch
        900-msysize.patch
        920-allow-win-drives-in-os-path-isabs.patch
        dont-make-libpython-readonly.patch
        00155-avoid-ctypes-thunks.patch
        00170-gc-assertions.patch)
sha256sums=('fb799134b868199930b75f26678f18932214042639cd52b16da7fd134cd9b13f'
            'de52e4722a6902e09dd6f343fdac0e7d45339f89386c8096b9c582c31aeb82e7'
            'b5a787f02811f46800f98bf242448770492643a4431771bb4283e6ae97016263'
            '4d7601b62e73c04553d22d480a873983a28b11df1f06be6898014453ce04afc3'
            'ff56bfe8dc1808484926c338fb5515d2d8cef36e95cc64d4940642a72276620f'
            '23bbe23afe90d5085ba7c27956c2df4ceee2acdfdc1d06a289070f045003fa09'
            '1ab20d38926aa85e638317620c29c484fcbbc228cd7cdf9543c29b750556d23f'
            '6ead37259110c28b16e35e05c93c82b46aa0fbb5152ff61bdaf1eea5f1ad01da'
            'f5b67408d39d3fda539ea90b12c48a05bd950b38460ec3e6a688e3377b071ed3'
            'b1d5a000bd5c9ec0a54f79e5487901d950e7685c57f50a5cea1a87012c034b4c'
            '4a6a4cdd0b3c8f0c3ae563005cc0a398d352c98bcb1826600f1ab4c3e0c24219'
            'e668b749e5f0bc584262add2cd7fffb2f6f32393a14918029d791012eda930f3'
            '879ef890846d900132c4283ea65351b060bc4760ca40fbe5e80ba5acbe98370c'
            'e3ef181333d5c9d20297849a46a68271a2190b7fc611c40c68c1ae240fa7ec36'
            '5b1083e9b50e149d623d863dee38ac1fb8d142f1bb78c8a01dcb09bfd97f4118'
            '35bf549bca1147a2a596132639d94f7b3a40de774780933e6a7f7fd9b0d309e3'
            '387a2b7931fb4958e2526991760d85677f44fa13cff0aeb0f41a267f1f7fd214'
            'f8b15d7079bfa1707e5bea78f600a0fca2077c25428c2bac5793b19b408f276e'
            '7a3f8f43b9c9eecb65d80b60c875344950a8b55082401830599d091548a3a985'
            'd8170bb446e5f3022bcc0eeb49f01d7c880161f1e115f4606e34554bf5ad0314')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}

prepare() {
  cd "${srcdir}/Python-${pkgver}"

  # FS#23997
  sed -i -e "s|^#.* /usr/local/bin/python|#!/usr/bin/python|" Lib/cgi.py

  apply_patch_with_msg \
    001-3.4-dbm-cygwin.patch \
    002-3.1-enable-new-dtags.patch \
    003-3.4-tkinter-cygwin.patch \
    004-3.4-ctypes-cygwin.patch \
    006-3.1-ncurses-abi6.patch \
    007-3.2-export-PySignal_SetWakeupFd.patch \
    009-3.2-distutils-shlibext.patch \
    010-3.6-pep3149-cygwin.patch \
    011-3.6-thread-cygwin64.patch \
    012-3.2-getpath-exe-extension.patch \
    013-3.4-select-cygwin.patch \
    016-3.6-ftm.patch \
    017-3.6-mpdec-cygwin.patch \
    025-MINGW-compiler-customize-mingw-cygwin-compilers.patch \
    900-msysize.patch \
    920-allow-win-drives-in-os-path-isabs.patch

  #archlinux
  apply_patch_with_msg dont-make-libpython-readonly.patch
  #fedora
  apply_patch_with_msg \
    00155-avoid-ctypes-thunks.patch \
    00170-gc-assertions.patch
  
  # Incomplete patch from Ray Donnelly
  # patch -p1 -i ${srcdir}/3.3.2-allow-windows-paths-for-executable.patch

  # Ensure that we are using the system copy of various libraries (expat, zlib and libffi),
  # rather than copies shipped in the tarball
  rm -r Modules/expat
  rm -r Modules/_ctypes/{darwin,libffi}*
  rm -r Modules/_decimal/libmpdec
  autoreconf -fiv
}

build() {
  cd "${srcdir}/Python-${pkgver}"

  CPPFLAGS+=" -I/usr/include/ncursesw"
  LDFLAGS+=" -L."

  CXX=/usr/bin/g++ \
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --enable-shared \
    --enable-ipv6 \
    --with-dbmliborder=gdbm \
    --with-libc= \
    --with-libm= \
    --with-system-expat \
    --with-system-ffi \
    --with-system-libmpdec \
    --enable-loadable-sqlite-extensions \
    --without-ensurepip \
    ac_cv_func_bind_textdomain_codeset=yes

  LC_CTYPE=en_US.UTF-8 make EXTRA_CFLAGS="$CFLAGS"
}

check() {
  cd "${srcdir}/Python-${pkgver}"
  "${srcdir}/Python-${pkgver}/python" -m test.regrtest -x test_posixpath test_logging
}

package() {
  cd "${srcdir}/Python-${pkgver}"
  make DESTDIR="${pkgdir}" EXTRA_CFLAGS="$CFLAGS" install maninstall

  # Why are these not done by default...
  ln -sf python3               "${pkgdir}"/usr/bin/python.exe
  ln -sf python3-config        "${pkgdir}"/usr/bin/python-config
  ln -sf idle3                 "${pkgdir}"/usr/bin/idle
  ln -sf pydoc3                "${pkgdir}"/usr/bin/pydoc
  ln -sf pyvenv-${_pybasever}  "${pkgdir}"/usr/bin/pyvenv3
  ln -sf python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python3.1
  ln -sf python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python.1

  # Fix FS#22552
  cp -f "${pkgdir}"/usr/lib/python${_pybasever}/config-${_pybasever}m/libpython${_pybasever}m.dll.a "${pkgdir}"/usr/lib/libpython${_pybasever}m.dll.a

  # Clean-up reference to build directory
  sed -i "s|$srcdir/Python-${pkgver}:||" "${pkgdir}/usr/lib/python${_pybasever}/config-${_pybasever}m/Makefile"

  # License
  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}
